<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
class Calon_manager extends CI_Controller {
    public static $link_view, $link_controller;
    function  __construct() {
        parent::__construct();
        $this->load->model('Arisan_model');
        $this->load->model('Cm_model');
        $this->load->model('Unit_model');
        $this->load->model('User_model');
        $this->load->model('Cabang_model');
		$this->load->model('Wilayah_model');
        self::$link_view = 'admin_views/calon_manager';
        $data['link_view'] = self::$link_view;
        $this->load->helper(array('html','form', 'url','jq_plugin'));
        $this->load->library('form_validation');


    }


    var $limit = 10;



    function index($offset='') {

        if ($this->session->userdata('is_logged_in') == TRUE) {
             $this->load->view('admin_views/calon_manager/index');
        }
        else {
            redirect('login');
        }

    }
//

     function home(){

			$data['form_action']	= site_url('calon_manager/register_proccess');
            $data['form_action2']	= site_url('calon_manager/register');

            $terpilih = date('m');
            $var="bulan";
            $bulan=1;
            $b=12;
            $data['combo_bulan'] = $this->combonamabln($bulan, $b, $var, $terpilih);

            $this->load->view('admin_views/calon_manager/register', $data);

    }

    function calon($offset='') {
        $data['form_action']	= site_url('calon_manager/index');
        $this->load->view('admin_views/calon_manager/calon', $data);
    }

    function breakdown($offset='') {
        $data['form_action']	= site_url('calon_manager/index');
        $this->load->view('admin_views/calon_manager/breakdown', $data);
    }

    function register() {
		$cabang = $this->User_model->getCabang($this->session->userdata('username'));
        $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);
		$data['cabang'] = $nm_cabang[0]->strnama_cabang;
            
		
		$data['form_action']	= site_url('calon_manager/register_proccess');
        $data['form_action2']	= site_url('calon_manager/register');
        // Load data
        if($_POST) {
            $data['register'] = $this->Cm_model->get_all_register_filter($_POST);

        }else {
            $data['register'] = $this->Cm_model->get_all_register();
            $num_rows = $this->Cm_model->count_all_pra();
        }
        $terpilih = date('m');
        $var="bulan";
        $bulan=1;
        $b=12;
		$data['bulanDaftar'] = $this->Cm_model->get_date_registerCalonManager();
        $data['combo_bulan'] = $this->combonamabln($bulan, $b, $var, $terpilih);
        $data['namaBulan'] = $this->nama_bulan($data['bulanDaftar']);
		$this->load->view('admin_views/calon_manager/register', $data);
		
		
    }
    function register_proccess() {
		$cek = "";
        
		$data['form_action2']	= site_url('calon_manager/register');
        $data['form_action']	= site_url('calon_manager/register_proccess');
        $this->form_validation->set_rules('strkode_dealer', 'strkode_dealer', 'required|callback_valid_dealer');
        $this->form_validation->set_rules('intid_unit', 'Unit', 'required');
        $this->form_validation->set_rules('strnama_dealer', 'Dealer', 'required');

        if ($this->form_validation->run() == TRUE) {
            // save data
            $cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $cabang = $nm_cabang[0]->strnama_cabang;
            $cm = array(
                    'cabang'            =>  $cabang,
                    'strkode_dealer' 		=> $this->input->post('strkode_dealer'),
                    'strkode_upline'		=> $this->input->post('strkode_upline'),
                    'strkode_manager'		=> $this->input->post('strkode_manager'),
                    'intid_unit'		=> $this->input->post('intid_unit'),
                    'tanggal'		=> date('Y-m-d'),
                    'bulan'         => $this->input->post('bulan_daftar'),
                    'is_pra'        => 1
            );

            $cek = $this->Cm_model->add($cm);
			if($cek == 1){
				$this->session->set_flashdata('message', 'Register Calon Manager Berhasil Disimpan, <br>Silahkan Melanjutkan Seleksi untuk Menjadi Pra Calon Manager');
            }elseif($cek == 2){
				$this->session->set_flashdata('message', 'Register Calon Manager Berhasil Disimpan Kembali!, <br>Silahkan Melanjutkan Seleksi untuk Menjadi Pra Calon Manager');
			}else{
				$this->session->set_flashdata('message', 'GAGAL TERSIMPAN! lihat kembali data calon manager ');
			}
			redirect('calon_manager/register');

        }
        else {
           	$this->session->set_flashdata('message', 'GAGAL TERSIMPAN! lihat kembali data calon manager ');
			 redirect('calon_manager/register');
            //$this->load->view('admin_views/calon_manager/register', $data);
        }

    }

    function nama_bulan($bulan) {
        switch ($bulan) {
            case '1':
                $b = "Januari";
                break;
            case '2':
                $b = "Februari";
                break;
            case '3':
                $b = "Maret";
                break;
            case '4':
                $b = "April";
                break;
            case '5':
                $b = "Mei";
                break;
            case '6':
                $b = "Juni";
                break;
            case '7':
                $b = "Juli";
                break;
            case '8':
                $b = "Agustus";
                break;
            case '9':
                $b = "September";
                break;
            case '10':
                $b = "Oktober";
                break;
            case '11':
                $b = "November";
                break;
            case '12':
                $b = "Desember";
                break;

        }

        return $b;

    }

    function simpan_manager() {

        if($this->input->post('ajax') == '1') {

            //$this->form_validation->set_rules('strkode_dealer', 'Nama', 'trim|required|xss_clean');
            $this->form_validation->set_rules('unit', 'unit', 'required|callback_valid_unit');
            $this->form_validation->set_message('required', 'Nama Unit Tidak Boleh Kosong');
            if($this->form_validation->run() == FALSE) {
                echo validation_errors();

            }else {
				//add Unit
                $cek = "";
				$cek = $this->Unit_model->insert($_POST);
				$id_unit =$this->db->insert_id();
                if($cek == true){
					//echo $id_unit;
					//if($id_unit) {
						
					$m = $this->Cm_model->get_dealer_by_id($this->input->post('strkode_dealer'));
					$jum = $m->result();
					$j = $jum[0]->intlevel_dealer - 1;
					$par = $jum[0]->intparent_leveldealer - 1;
					
					//add baby manager Unit
					$unit_awal = $jum[0]->intid_unit;
					$this->Unit_model->insert_babymanager($id_unit, $unit_awal);
					
					//update Manager
					$cm = array(
							'is_pra'=>0,
							'is_cm'=>0,
							'is_manager' 		=> 1,
							'intid_unitbaru'    => $id_unit,
							'tanggal_lulus'		=> date('Y-m-d')
					);

					$this->Cm_model->update_calon($this->input->post('strkode_dealer'), $cm);
					
					//update status downline dan akarnya
					$this->Cm_model->rekursif_downline_update($this->input->post('strkode_dealer'),$j,$jum[0]->intlevel_dealer,$jum[0]->intparent_leveldealer,'>',$id_unit);					
					
					//update status di Member
					$level = array(
							'intparent_leveldealer' => $par,
							'intlevel_dealer' => $j,
							'intid_unit' => $id_unit,
							'strkode_upline' => '',
					);

					$this->Cm_model->update($this->input->post('strkode_dealer'), $level);
					/*
					$downline = $this->Cm_model->getDownline($this->input->post('strkode_dealer'));
					foreach($downline as $down) {
						$lev = $down->intlevel_dealer - 1;
						$parent_level = $down->intparent_leveldealer - 1;
						$level = array(
								'intparent_leveldealer' => $parent_level,
								'intlevel_dealer' => $lev,
								'intid_unit' => $id_unit,
						);
						//print_r($lev);
						$this->Cm_model->update($down->strkode_dealer, $level);

					}
					*/
					
					$data['form_action'] = site_url('calon_manager/calon');
					$data['status'] = "sukses";
					//$this->session->set_flashdata('message2', 'Unit Baru telah Tersimpan, Silahkan Klik <input type=submit name=submit value=OK> untuk menyimpan');
					$this->load->view('admin_views/calon_manager/finish', $data);
					//redirect('calon_manager');
				}else{
					redirect('calon_manager');
				}
            }
        }
    }

    function last_selection() {
        if($this->input->post('ajax') == '1') {

            $this->form_validation->set_rules('strkode_dealer', 'Nama', 'trim|required|xss_clean');
            $this->form_validation->set_message('required', 'Please fill in the fields');
            if($this->form_validation->run() == FALSE) {
                echo validation_errors();
            }else {
                $data['form_action'] = site_url('calon_manager/simpan_manager');
                $data['strkode'] = $this->input->post('strkode_dealer');

                //update CM
                $cm = array(
                        'is_pra'    =>0,
                        'is_cm' 		=> 1,
                        'cm_khusus' => $this->input->post('khusus'),
                        'total_omset' => $this->input->post('total_omset'),
                        'total_omset' => $this->input->post('total_pv'),
                        'tanggal'		=> date('Y-m-d')
                );

                $this->Cm_model->update_calon($this->input->post('strkode_dealer'), $cm);

                $this->load->view('admin_views/calon_manager/last_selection', $data);
            }
        }
    }

    function check_downline() {
        if($this->input->post('ajax') == '1') {

            $this->form_validation->set_rules('strkode_dealer', 'Nama', 'trim|required|xss_clean');
            $this->form_validation->set_message('required', 'Please fill in the fields');
            if($this->form_validation->run() == FALSE) {
                echo validation_errors();
            } else {
                $data['downline'] = $this->Cm_model->getDownline($this->input->post('strkode_dealer'));
                $data['strkode'] = $this->input->post('strkode_dealer');

                $b = $this->Cm_model->getMonth($this->input->post('strkode_dealer'));
                $bulan = $b[0]->bul;

                $a = $bulan + 1;
                if($a == 13) {
                    $b = 1;
                }else {
                    $b = $a;
                }

                $aa = $bulan + 2;
                if($aa == 14) {
                    $b = 2;
                }else {
                    $b = $aa;
                }
                $var = "bulan";

                $terpilih = date('m');
                $data['combo_bulan'] = $this->combonamabln($bulan, $b, $var, $terpilih);

                $this->load->view('admin_views/calon_manager/downline', $data);
            }
        }
    }

    function combonamabln($awal, $akhir, $var, $terpilih) {
        $nama_bln=array(1=> "Januari", "Februari", "Maret", "April", "Mei",
                "Juni", "Juli", "Agustus", "September",
                "Oktober", "November", "Desember");
        $b='';
        $b.="<select name=$var id=$var>";
        for ($bln=$awal; $bln<=$akhir; $bln++) {
            if ($bln==$terpilih)
                $b.="<option value=$bln selected>$nama_bln[$bln]</option>";
            else
                $b.="<option value=$bln>$nama_bln[$bln]</option>";
        }
        $b.="</select> ";
        return $b;
    }

    function cm1() {
        $tahun = $this->input->post('tahun');
        $bulan = $this->input->post('bulan');

        $a = $bulan + 1;
        if($a == 13) {
            $b = 1;
        }else {
            $b = $a;
        }
        $aa = $bulan + 2;
        if($aa == 14) {
            $c = 2;
        }else {
            $c = $aa;
        }
		$aaa = $bulan + 3;
        if($aa == 15) {
            $d = 3;
        }else {
            $d = $aaa;
        }
        

        $khusus = $this->input->post('khusus');
        if($this->input->post('ajax') == '1') {

            $this->form_validation->set_rules('khusus', 'Bulan', 'required');
            
            $this->form_validation->set_message('required', 'Please fill in the fields');
            if($this->form_validation->run() == FALSE) {
                echo validation_errors();
            } else {
                $nama_bulan = $this->nama_bulan($b);
                $nama_bulan1 = $this->nama_bulan($c);
                $nama_bulan2 = $this->nama_bulan($d);

                $data['downline'] = $this->Cm_model->getDownline($this->input->post('strkode_dealer'));
                $data['bulan'] = $nama_bulan;
                $data['bulan1'] = $nama_bulan1;
                $data['bulan2'] = $nama_bulan2;
                $data['form_action'] = site_url('calon_manager/simpan_manager');
                $data['bln'] = $bulan;
                $data['tahun'] = $this->input->post('tahun');
                $data['strkode'] = $this->input->post('strkode_dealer');

                //update Pra CM
                $cm = array(
                        'is_pra' 		=> 1,
						'tanggal'		=> date('Y-m-d')
                );

                $this->Cm_model->update_calon($this->input->post('strkode_dealer'), $cm);
				
				 $cmmember = array(
                        'is_cm' 		=> 1
                );

                $this->Cm_model->update_member($this->input->post('strkode_dealer'), $cmmember);

                $id = $this->Cm_model->getidDealer($this->input->post('strkode_dealer'));
                $getTanggal = $this->Cm_model->getWeek1($id[0]->intid_dealer);
                $data['weeks'] = $this->Cm_model->cek_bulan($b, $tahun);
                $data['penjualan_luar_rekrut'] = $this->Cm_model->luar_rekrut($this->input->post('strkode_dealer'), $data['weeks'], $b, $tahun); 
			    
				if(empty($data['weeks'])) {
                    $data['error'] = "Belum Ada Settingan Kalender di Bulan Selanjutnya";
                }else {

                    foreach ($data['weeks'] as $t) {
                        $data['week_pv'] = $this->Cm_model->week_pv($this->input->post('strkode_dealer'), $b);
						$data['week_pv_pribadi'] = $this->Cm_model->week_pv_pribadi($this->input->post('strkode_dealer'), $b);
                        $data['week_pribadi'][] = $this->Cm_model->weeks1($id[0]->intid_dealer, $t->intid_week);
                        $data['week_rekrut'][] = $this->Cm_model->week_rekrut($this->input->post('strkode_dealer'), $t->intid_week);
                        $data['penjualanrekrut'][] = $this->Cm_model->penjualanrekrut($this->input->post('strkode_dealer'), $t->intid_week);
                    }
                }

                $data['weeks2'] = $this->Cm_model->cek_bulan($c, $tahun);
                
                $data['penjualan_luar_rekrut2'] = $this->Cm_model->luar_rekrut($id[0]->intid_dealer, $data['weeks2'], $c, $tahun);
                if(empty($data['weeks2'])) {
                    $data['error2'] = "Belum Ada Settingan Kalender di Bulan Selanjutnya";
                }else {

                    foreach ($data['weeks2'] as $t) {
                        $data['week_pv2'] = $this->Cm_model->week_pv($this->input->post('strkode_dealer'),$c);
						$data['week_pv_pribadi2'] = $this->Cm_model->week_pv_pribadi($this->input->post('strkode_dealer'), $c);
                        $data['week_pribadi2'][] = $this->Cm_model->weeks1($id[0]->intid_dealer, $t->intid_week);
                        $data['week_rekrut2'][] = $this->Cm_model->week_rekrut($this->input->post('strkode_dealer'), $t->intid_week);
                        $data['penjualanrekrut2'][] = $this->Cm_model->penjualanrekrut($this->input->post('strkode_dealer'), $t->intid_week);
                    }
                }

                $data['weeks3'] = $this->Cm_model->cek_bulan($d, $tahun);
                $data['penjualan_luar_rekrut3'] = $this->Cm_model->luar_rekrut($id[0]->intid_dealer, $data['weeks3'], $d, $tahun);
                if(empty($data['weeks3'])) {
                    $data['error3'] = "Belum Ada Settingan Kalender di Bulan Selanjutnya";
                }else {

                    foreach ($data['weeks3'] as $t) {
                        $data['week_pv3'] = $this->Cm_model->week_pv($this->input->post('strkode_dealer'),$d);
						$data['week_pv_pribadi3'] = $this->Cm_model->week_pv_pribadi($this->input->post('strkode_dealer'), $d);
                        $data['week_pribadi3'][] = $this->Cm_model->weeks1($id[0]->intid_dealer, $t->intid_week);
                        $data['week_rekrut3'][] = $this->Cm_model->week_rekrut($this->input->post('strkode_dealer'), $t->intid_week);
                        $data['penjualanrekrut3'][] = $this->Cm_model->penjualanrekrut($this->input->post('strkode_dealer'), $t->intid_week);
                    }
                }
                $data['khusus'] = $this->input->post('khusus');
                if($khusus==1) {
                    $this->load->view('admin_views/calon_manager/cm1', $data);
                }elseif($khusus==2) {
                    $this->load->view('admin_views/calon_manager/cm2', $data);
                }elseif($khusus==3) {
					$data['form_action'] = site_url('calon_manager/reset_pra');
                    $this->load->view('admin_views/calon_manager/cm3', $data);
                }
            }
        }
    }

    function pra_cm() {
        $bulan = $this->input->post('bulan');
        $tahun = $this->input->post('tahun');
        $khusus = $this->input->post('khusus');
        $a = $bulan + 1;
        if($a == 13) {
            $b = 1;
        }else {
            $b = $a;
        }
        $aa = $bulan + 2;
        if($aa == 14) {
            $c = 2;
        }else {
            $c = $aa;
        }
        //print_r($b);
        $nama_bulan = $this->nama_bulan($bulan);
        $nama_bulan1 = $this->nama_bulan($b);
        $nama_bulan2 = $this->nama_bulan($c);

        if($this->input->post('ajax') == '1') {
            $this->form_validation->set_rules('bulan', 'Bulan', 'required');
            $this->form_validation->set_rules('tahun', 'Tahun', 'required');

            //$this->form_validation->set_message('required', 'Please fill in the fieldsss');

            if($this->form_validation->run() == FALSE) {
                echo validation_errors();
            } else {
                $data['downline'] = $this->Cm_model->getDownline($this->input->post('strkode_dealer'));
                $data['bulan'] = $nama_bulan;
                $data['bulan1'] = $nama_bulan1;
                $data['bulan2'] = $nama_bulan2;
                $data['bln'] = $bulan;
                $data['tahun'] = $this->input->post('tahun');
                $data['strkode'] = $this->input->post('strkode_dealer');
                $data['form_action'] = site_url('calon_manager/reset_pra');
                $id = $this->Cm_model->getidDealer($this->input->post('strkode_dealer'));
                $getTanggal = $this->Cm_model->getWeek1($id[0]->intid_dealer);
                $data['weeks'] = $this->Cm_model->cek_bulan($bulan, $tahun);

                if(empty($data['weeks'])) {
                    $data['error'] = "Belum Ada Settingan Kalender di Bulan Selanjutnya";
                }else {
                    foreach ($data['weeks'] as $t) {
                        //print_r($t);
                        
                        $data['week_pribadi'][] = $this->Cm_model->weeks1($id[0]->intid_dealer, $t->intid_week);
                        $data['week_rekrut'][] = $this->Cm_model->week_rekrut($this->input->post('strkode_dealer'), $t->intid_week);
                        $data['penjualanrekrut'][] = $this->Cm_model->penjualanrekrut($this->input->post('strkode_dealer'), $t->intid_week);
                    }
                }

                $this->load->view('admin_views/calon_manager/pra_cm', $data);
            }
        }
    }

    function weekNumberOfMonth($date) {
        $tgl=date_parse($date);
        //$tgl = explode("-", $date);
        $tanggal =  $tgl['day'];
        $bulan   =  $tgl['month'];
        $tahun   =  $tgl['year'];
        //tanggal 1 tiap bulan
        $tanggalAwalBulan = mktime(0, 0, 0, $bulan, 1, $tahun);
        $mingguAwalBulan = (int) date('W', $tanggalAwalBulan);
        //tanggal sekarang
        $tanggalYangDicari = mktime(0, 0, 0, $bulan, $tanggal, $tahun);
        $mingguTanggalYangDicari = (int) date('W', $tanggalYangDicari);
        $mingguKe = $mingguTanggalYangDicari - $mingguAwalBulan + 1;
        return $mingguKe;
    }


    function lookupBarang() {
        $keyword = $this->input->post('term');
        $data['response'] = 'false';
        $query = $this->Arisan_model->selectBarang($keyword);
        if( ! empty($query) ) {
            $data['response'] = 'true';
            $data['message'] = array();
            foreach( $query as $row ) {
                $data['message'][] = array(
                        'id'=>$row->intid_barang,
                        'value' => $row->strnama,
                        'value1' => $row->intharga_jawa,
                );
            }

        }


        if('IS_AJAX') {
            echo json_encode($data);

        }
        else {
            $this->load->view('admin_views/autocomplete/index',$data);
        }
    }

    function view_rekrut1($strkode, $bulan, $tahun) {
        $id = $this->Cm_model->getidDealer($strkode);
        $getTanggal = $this->Cm_model->getWeek1($id[0]->intid_dealer);

        $data['viewpenjualanrekrut1'] = $this->Cm_model->viewpenjualanrekrut1($id[0]->intid_dealer,
                $strkode,
                $bulan,
                $tahun);
        //print_r($data['viewpenjualanrekrut1']);
        $this->load->view('admin_views/calon_manager/view_rekrut1', $data);
    }

    function view_rekrut2() {
        /*        $id = $this->Cm_model->getidDealer($strkode);
        $getTanggal = $this->Cm_model->getWeek1($id[0]->intid_dealer);

        $data['viewpenjualanrekrut1'] = $this->Cm_model->viewpenjualanrekrut2($id[0]->intid_dealer,
                $strkode,
                $bulan,
                $tahun);*/
        //print_r($data['viewpenjualanrekrut1']);
        $data['expandable'] = 'true';
        $this->load->view('admin_views/calon_manager/view_rekrut2', $data);
    }

    function ranting() {
        if($this->input->post('ajax') == '1') {

            $this->form_validation->set_rules('strkode_dealer', 'Nama', 'trim|required|xss_clean');
            $this->form_validation->set_message('required', 'Please fill in the fields');
            if($this->form_validation->run() == FALSE) {
                echo validation_errors();
            } else {
                $data['strnama_dealer'] = $this->Cm_model->getMember($this->input->post('strkode_dealer'));
                $data['strkode_upline'] = $this->input->post('strkode_dealer');

                $this->load->view('admin_views/calon_manager/ranting', $data);
            }
        }
        //$data['expandable'] = 'true';
        //$this->load->view('admin_views/calon_manager/ranting', $data);
    }

    function update_breakdown($strkode_dealer, $strkode_upline, $unit, $unit_baru) {

        $data['strnama_dealer'] = $this->Cm_model->getMember($this->input->post('strkode_dealer'));
        $data['strkode_upline'] = $this->input->post('strkode_dealer');

        $cabang = $this->User_model->getCabang($this->session->userdata('username'));
        $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);
        $cabang = $nm_cabang[0]->strnama_cabang;

        $this->Unit_model->insert($_POST);
        $id_unit =$this->db->insert_id();

        $cm = array(
                'cabang'            =>  $cabang,
                'strkode_dealer' 		=> $strkode_dealer,
                'strkode_upline'		=> $strkode_upline,
                'strkode_manager'		=> $strkode_upline,
                'intid_unit'		=> $unit,
                'intid_unitbaru'		=> $id_unit,
                'tanggal'		=> date('Y-m-d'),
                'is_breakdown'        => 1
        );

        $this->Cm_model->add($cm);

       
                
        $m = $this->Cm_model->get_dealer_by_id($strkode_dealer);
        $jum = $m->result();
        $j = $jum[0]->intlevel_dealer - 1;
        $par = $jum[0]->intparent_leveldealer - 1;

        $level = array(
                'intparent_leveldealer' => $par,
                'intlevel_dealer' => $j,
                'intid_unit' => $id_unit,
                'strkode_upline' => '',
        );

        $this->Cm_model->update($strkode_dealer, $level);

        $downline = $this->Cm_model->getDownline(strkode_dealer);

        foreach($downline as $down) {
            $lev = $down->intlevel_dealer - 1;
            $parent_level = $down->intparent_leveldealer - 1;
            $level = array(
                    'intparent_leveldealer' => $parent_level,
                    'intlevel_dealer' => $lev,
                    'intid_unit' => $id_unit,
            );
            //print_r($lev);
            $this->Cm_model->update($down->strkode_dealer, $level);

        }

    }



    function view_rekrut3($strkode, $week) {
        $data['viewpenjualanrekrut1'] = $this->Cm_model->viewpenjualanrekrut1($strkode,$week);
        //print_r($data['viewpenjualanrekrut1']);
        $this->load->view('admin_views/calon_manager/view_rekrut3', $data);
    }

    function view_rekrut4($strkode, $week) {
        $id = $this->Cm_model->getidDealer($strkode);

        $data['viewpenjualanrekrut1'] = $this->Cm_model->viewpenjualanrekrut1($strkode,$week);
        $this->load->view('admin_views/calon_manager/view_rekrut1', $data);
    }

    function view_nota($nota) {
        $data['view_nota'] = $this->Cm_model->viewDetailnota($nota);
        $this->load->view('admin_views/calon_manager/view_detail_nota', $data);
    }

    function data_pra($offset = 0) {
        // Offset
        $uri_segment = 3;
        $offset = $this->uri->segment($uri_segment);
        $data['form_action']	= site_url('calon_manager/data_pra');
		$data['combo_bulan'] = $this->combonamabln(1, 12, "bulan",date('m'));
		$var = $this->input->post('cari_nama');
        // Load data
        if($_POST) {
			//	$data['pra_cm'] = $this->Cm_model->get_all_pra_filter($_POST);
				$data['pra_cm'] = $this->Cm_model->get_all_pra_filter_lagi($_POST);
			
        }else {
            $data['pra_cm'] = $this->Cm_model->get_all_pra_bulan(date('m'));
            $num_rows = $this->Cm_model->count_all_pra();
        }


        // Load view
        $this->load->view('admin_views/calon_manager/data_pra', $data);
    }

    function batal_pra($id) {
        $data['form_action']	= site_url('calon_manager/data_pra');
        $cm = array(
                'is_pra' 		=> 0
        );

        $this->Cm_model->update_calon($id, $cm);
        $this->load->view('admin_views/calon_manager/data_pra', $data);
    }

    function data_cmkhusus($offset = 0) {
        $data['form_action']	= site_url('calon_manager/data_cm');

        if($_POST) {
            $data['pra_cm'] = $this->Cm_model->get_all_cm_filter($_POST);

        }else {
            $data['pra_cm'] = $this->Cm_model->get_all_cm();
            $num_rows = $this->Cm_model->count_all_pra();
        }
        // Load view
        $this->load->view('admin_views/calon_manager/data_cm', $data);
    }

    function data_manager($offset = 0) {
        $data['form_action']	= site_url('calon_manager/data_manager');


        if($_POST) {
            $data['pra_cm'] = $this->Cm_model->get_all_manager_filter($_POST);
            $data['bulan'] = $this->input->post('bulan');
            $data['nama_bulan'] = $this->nama_bulan($this->input->post('bulan'));
            $data['tahun'] = $this->input->post('tahun');
            $bln = $this->input->post('bulan');
        }else {
            $data['pra_cm'] = $this->Cm_model->get_all_manager();
            $data['bulan'] = "NULL";
            $data['tahun'] = "NULL";
            $bln=date('m');
        }

        $terpilih = $bln;
        $var = "bulan";
        $bulan = 1;
        $b = 12;
        $data['combo_bulan'] = $this->combonamabln($bulan, $b, $var, $terpilih);

        $this->load->view('admin_views/calon_manager/data_manager', $data);

        $this->load->library('html2pdf');
        $this->html2pdf->folder('./assets/pdfs/');
        $this->html2pdf->filename('cm.pdf');
        $this->html2pdf->paper('a4', 'landscape');
        $this->html2pdf->html($this->load->view('admin_views/calon_manager/cetak_manager', $data, true));
        $this->html2pdf->create('save');
    }

    function cetak_manager($kode) {

        //Load the library
        $this->load->library('html2pdf');

        //Set folder to save PDF to
        $this->html2pdf->folder('./assetss/pdfs/');

        //Set the filename to save/download as
        $this->html2pdf->filename('test.pdf');

        //Set the paper defaults
        $this->html2pdf->paper('a4', 'portrait');

        $data = array(
                'title' => 'PDF Created',
                'message' => 'Hello World!'
        );

        //Load html view
        $this->html2pdf->html($this->load->view('admin_views/calon_manager/cetak_manager', $data, true));
        //$this->load->view('admin_views/calon_manager/cetak_manager', $data, true);
        if($this->html2pdf->create('save')) {
            //PDF was successfully saved or downloaded
            $this->load->view('admin_views/calon_manager/cetak_manager', $data);
            //echo 'PDF saved';
        }

    }

    function batal_manager($id) {
        $data['form_action']	= site_url('calon_manager/data_manager');
        $cm = array(
                'is_manager' 		=> 0
        );

        $this->Cm_model->update_calon($id, $cm);
        $this->load->view('admin_views/calon_manager/data_manager', $data);
    }

    function batal_cmkhusus($id) {
        $data['form_action']	= site_url('calon_manager/data_cm');
        $cm = array(
                'is_cm' 		=> 0
        );
        $this->Cm_model->update_calon($id, $cm);
        $this->load->view('admin_views/calon_manager/data_cm', $data);
    }

    function hapus_register($id) {
        $this->Cm_model->delete($id);
        $this->session->set_flashdata('message', '1 data Register berhasil dihapus');

        redirect('calon_manager/register');
    }

    function reset_pra() {
		$id = $this->input->post('strkode_dealer');
        $this->Cm_model->delete_pra($id);
        $this->session->set_flashdata('message', 'Membatalkan Pra Manager/Calon Manager, Untuk Pengajuan Silahkan untuk Didaftarkan Kembali');

        redirect('calon_manager/register');
    }

    function valid_dealer($kode) {
        if ($this->Cm_model->valid_dealer($kode) == TRUE) {
            $this->form_validation->set_message('valid_dealer', "Calon Manager dengan Kode Dealer $kode sudah terdaftar");
            return FALSE;
        }
        else {
            return TRUE;
        }
    }

    function valid_unit($kode) {
        if ($this->Cm_model->valid_unit($kode) == TRUE) {
            $this->form_validation->set_message('valid_unit', "Nama Unit $kode sudah terdaftar");
            return FALSE;
        }
        else {
            return TRUE;
        }
    }
	
	function jum_children ($induk, $level) {
    	$query = "select member.intid_dealer, member.strkode_upline, member.strkode_dealer, member.strnama_dealer, sum(nota.inttotal_omset)as inttotal_omset from member left join nota on nota.intid_dealer = member.intid_dealer where member.strkode_upline='$induk' group by member.intid_dealer";
		$result = $this->db->query($query);
		$l = $level;
		if($result->num_rows() > 0):
			foreach($result->result_array() as $row):
					$child = $this->jum_children($row['strkode_dealer'],$level+1);
					if($child > 1 && $l < $child) $l = $child;	
			endforeach;
		endif;
    	
		
		return $l;
					
	}
	function lihat_excel_pracm(){
		$cabang = $this->User_model->getCabang($this->session->userdata('username'));
        $data['default'] = $this->Cabang_model->select($cabang[0]->intid_cabang);
        
		$this->load->model('cm_model');
		$data['bulan'] = $this->input->post('bulan');
		$data['dataexcel'] = $this->cm_model->data_pracm_bulan($data['bulan']);
		$this->load->view('admin_views/calon_manager/lihat_excel_datapracm', $data);
	}
	/*
	function rekursif(){
		$this->Cm_model->rekursif_downline_update('M780165','2','1','0','>','unit');
	}
	*/
}
?>
